home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / emacs / sshell.el < prev   
Encoding:
Text File  |  2003-02-09  |  13.0 KB  |  379 lines

  1.  
  2. ;; Run subshell under Emacs
  3. ;; Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
  4. ;; Modifications by William Schelter
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 1, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;; The following is a "simple shell" much like the one in version 18
  22. ;; of emacs.   Unfortunately cmint breaks most code which tries to use
  23. ;; the shell mode, and is rather complex.
  24.  
  25. ;; This mode uses a better completion mechanism (smart-complete.el),
  26. ;; in that it should
  27. ;; find the input you really want with your typing less keystrokes,
  28. ;; and easier keystrokes to type
  29.  
  30.  
  31.  
  32.  
  33.  
  34. (defvar last-input-start nil
  35.   "In a sshell-mode buffer, marker for start of last unit of input.")
  36. (defvar last-input-end nil
  37.   "In a sshell-mode buffer, marker for end of last unit of input.")
  38.  
  39. (defvar sshell-mode-map nil)
  40.  
  41. (defvar sshell-directory-stack nil
  42.   "List of directories saved by pushd in this buffer's sshell.")
  43.  
  44. (defvar sshell-popd-regexp "popd"
  45.   "*Regexp to match subsshell commands equivalent to popd.")
  46.  
  47. (defvar sshell-pushd-regexp "pushd"
  48.   "*Regexp to match subsshell commands equivalent to pushd.")
  49.  
  50. (defvar sshell-cd-regexp "cd"
  51.   "*Regexp to match subsshell commands equivalent to cd.")
  52.  
  53. (defvar explicit-sshell-file-name nil
  54.   "*If non-nil, is file name to use for explicitly requested inferior sshell.")
  55.  
  56.  
  57. ;In loaddefs.el now.
  58. (defconst sshell-prompt-pattern
  59.  "\\(^\\|\n\\)[^ >]*[>$)%#:][>]*[ ]*"
  60.   "*Regexp used by Newline command to match subsshell prompts.
  61. Anything from beginning of line up to the end of what this pattern matches
  62. is deemed to be prompt, and is not reexecuted.")
  63.  
  64. (defun sshell-mode ()
  65.   "Major mode for interacting with an inferior sshell.
  66. Sshell name is same as buffer name, sans the asterisks.
  67. Return at end of buffer sends line as input.
  68. Return not at end copies rest of line to end and sends it.
  69.  
  70. The following commands imitate the usual Unix interrupt and
  71. editing control characters:
  72. \\{sshell-mode-map}
  73.  
  74. Entry to this mode calls the value of sshell-mode-hook with no args,
  75. if that value is non-nil.
  76.  
  77. cd, pushd and popd commands given to the sshell are watched
  78. by Emacs to keep this buffer's default directory
  79. the same as the sshell's working directory.
  80. Variables sshell-cd-regexp, sshell-pushd-regexp and sshell-popd-regexp
  81. are used to match these command names.
  82.  
  83. You can send text to the sshell (or its subjobs) from other buffers
  84. using the commands process-send-region, process-send-string
  85. and lisp-send-defun."
  86.   (interactive)
  87.   (kill-all-local-variables)
  88.   (setq major-mode 'sshell-mode)
  89.   (setq mode-name "Sshell")
  90.   (setq mode-line-process '(": %s"))
  91.   (use-local-map sshell-mode-map)
  92.   (make-local-variable 'sshell-directory-stack)
  93.   (setq sshell-directory-stack nil)
  94.   (make-local-variable 'last-input-start)
  95.   (setq last-input-start (make-marker))
  96.   (make-local-variable 'last-input-end)
  97.   (setq last-input-end (make-marker))
  98.   (run-hooks 'sshell-mode-hook))
  99.  
  100. (if sshell-mode-map
  101.     nil
  102.   (setq sshell-mode-map (make-sparse-keymap))
  103.   (define-key sshell-mode-map "\t" 'sshell-complete-filename)
  104.   (define-key sshell-mode-map "\C-m" 'sshell-send-input)
  105.   (define-key sshell-mode-map "\C-c\C-d" 'sshell-send-eof)
  106.   (define-key sshell-mode-map "\C-c\C-u" 'kill-sshell-input)
  107.   (define-key sshell-mode-map "\C-c\C-w" 'backward-kill-word)
  108.   (define-key sshell-mode-map "\C-c\C-c" 'interrupt-sshell-subjob)
  109.   (define-key sshell-mode-map "\C-c\C-z" 'stop-sshell-subjob)
  110.   (define-key sshell-mode-map "\C-c\C-\\" 'quit-sshell-subjob)
  111.   (define-key sshell-mode-map "\C-c\C-o" 'kill-output-from-sshell)
  112.   (define-key sshell-mode-map "\C-c\C-r" 'show-output-from-sshell)
  113.   (define-key sshell-mode-map "\C-c\C-y" 'copy-last-sshell-input))
  114.  
  115.  
  116. (defun sshell-complete-filename ()
  117.   (interactive)
  118.   (let* ((p (point))  tem beg
  119.      (ff
  120.       (save-excursion
  121.         (skip-chars-backward "[a-z---_0-9$/A-Z~#.]")
  122.         (buffer-substring (setq beg (point)) p))))
  123.     (setq dir (or (file-name-directory ff) default-directory))
  124.     (setq file (file-name-nondirectory ff))
  125.     (cond ((and (setq tem (file-name-completion (or file "") dir))
  126.         (not (equal tem file)))
  127.        (cond ((eq tem t))
  128.          (t
  129.           (delete-region beg p)
  130.           (insert (concat dir tem)))))
  131.       (t
  132.        (let ((lis (file-name-all-completions file dir)))
  133.          (with-output-to-temp-buffer "*completions*"
  134.            (display-completion-list    lis))
  135.          )))))
  136.  
  137. (defvar explicit-csh-args
  138.   (if (eq system-type 'hpux)
  139.       ;; -T persuades HP's csh not to think it is smarter
  140.       ;; than us about what terminal modes to use.
  141.       '("-i" "-T")
  142.     '("-i"))
  143.   "Args passed to inferior sshell by M-x sshell, if the sshell is csh.
  144. Value is a list of strings, which may be nil.")
  145.  
  146.  
  147. (defun sshell ()
  148.   "Run an inferior sshell, with I/O through buffer *sshell*.
  149. If buffer exists but sshell process is not running, make new sshell.
  150. Program used comes from variable explicit-sshell-file-name,
  151.  or (if that is nil) from the ESHELL environment variable,
  152.  or else from SHELL if there is no ESHELL.
  153. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  154.  (Note that this may lose due to a timing error if the sshell
  155.   discards input when it starts up.)
  156. The buffer is put in sshell-mode, giving commands for sending input
  157. and controlling the subjobs of the sshell.  See sshell-mode.
  158. See also variable sshell-prompt-pattern.
  159.  
  160. The sshell file name (sans directories) is used to make a symbol name
  161. such as `explicit-csh-arguments'.  If that symbol is a variable,
  162. its value is used as a list of arguments when invoking the sshell.
  163. Otherwise, one argument `-i' is passed to the sshell.
  164.  
  165. Note that many people's .cshrc files unconditionally clear the prompt.
  166. If yours does, you will probably want to change it."
  167.   (interactive)
  168.   (let* ((prog (or explicit-sshell-file-name
  169.            (getenv "ESHELL")
  170.            (getenv "SHELL")
  171.            "/bin/sh"))             
  172.      (name (file-name-nondirectory prog)))
  173.     (switch-to-buffer
  174.      (apply 'make-sshell "shell" prog
  175.         (if (file-exists-p (concat "~/.emacs_" name))
  176.         (concat "~/.emacs_" name))
  177.         (let ((symbol (intern-soft (concat "explicit-" name "-args"))))
  178.           (if (and symbol (boundp symbol))
  179.           (symbol-value symbol)
  180.         '("-i")))))))
  181.  
  182. (defun make-sshell (name program &optional startfile &rest switches)
  183.   (let ((buffer (get-buffer-create (concat "*" name "*")))
  184.     proc status size)
  185.     (setq proc (get-buffer-process buffer))
  186.     (if proc (setq status (process-status proc)))
  187.     (save-excursion
  188.       (set-buffer buffer)
  189.       ;;    (setq size (buffer-size))
  190.       (if (memq status '(run stop))
  191.       nil
  192.     (if proc (delete-process proc))
  193.     (setq proc (apply 'start-process name buffer
  194.                (or program explicit-sshell-file-name
  195.                   (getenv "ESHELL")
  196.                   (getenv "SHELL")
  197.                   "/bin/sh")
  198.               switches))
  199.     
  200.     (cond (startfile
  201.            ;;This is guaranteed to wait long enough
  202.            ;;but has bad results if the sshell does not prompt at all
  203.            ;;         (while (= size (buffer-size))
  204.            ;;           (sleep-for 1))
  205.            ;;I hope 1 second is enough!
  206.            (sleep-for 1)
  207.            (goto-char (point-max))
  208.            (insert-file-contents startfile)
  209.            (setq startfile (buffer-substring (point) (point-max)))
  210.            (delete-region (point) (point-max))
  211.            (process-send-string proc startfile)))
  212.     (setq name (process-name proc)))
  213.       (goto-char (point-max))
  214.       (set-marker (process-mark proc) (point))
  215.       (sshell-mode))
  216.     buffer))
  217.  
  218. (defvar sshell-set-directory-error-hook 'ignore
  219.   "Function called with no arguments when sshell-send-input
  220. recognizes a change-directory command but gets an error
  221. trying to change Emacs's default directory.")
  222.  
  223. (defun sshell-send-input ()
  224.   "Send input to subsshell.
  225. At end of buffer, sends all text after last output
  226.  as input to the subsshell, including a newline inserted at the end.
  227. When not at end, copies current line to the end of the buffer and sends it,
  228. after first attempting to discard any prompt at the beginning of the line
  229. by matching the regexp that is the value of sshell-prompt-pattern if possible.
  230. This regexp should start with \"^\"."
  231.   (interactive)
  232.   (or (get-buffer-process (current-buffer))
  233.       (error "Current buffer has no process"))
  234.   (end-of-line)
  235.   (if (eobp)
  236.       (progn
  237.     (move-marker last-input-start
  238.              (process-mark (get-buffer-process (current-buffer))))
  239.     (insert ?\n)
  240.     (move-marker last-input-end (point)))
  241.     (beginning-of-line)
  242.     ;; Exclude the sshell prompt, if any.
  243.     (re-search-forward sshell-prompt-pattern
  244.                (save-excursion (end-of-line) (point))
  245.                t)
  246.     (let ((copy (buffer-substring (point)
  247.                   (progn (forward-line 1) (point)))))
  248.       (goto-char (point-max))
  249.       (move-marker last-input-start (point))
  250.       (insert copy)
  251.       (move-marker last-input-end (point))))
  252.   ;; Even if we get an error trying to hack the working directory,
  253.   ;; still send the input to the subsshell.
  254.   (condition-case ()
  255.       (save-excursion
  256.     (goto-char last-input-start)
  257.     (sshell-set-directory))
  258.     (error (funcall sshell-set-directory-error-hook)))
  259.   (let ((process (get-buffer-process (current-buffer)))
  260.     (s (buffer-substring last-input-start last-input-end))
  261.     )
  262.     ;; avoid sending emacs's idea of what an international character
  263.     ;; set string is to a subprocess..
  264.     (if (fboundp 'string-make-unibyte)
  265.     (setq s (string-make-unibyte s)))
  266.     (process-send-string process s)
  267.     (set-marker (process-mark process) (point))))
  268.  
  269. ;;;  If this code changes (sshell-send-input and sshell-set-directory),
  270. ;;;  the customization tutorial in
  271. ;;;  info/customizing-tutorial must also change, since it explains this
  272. ;;;  code.  Please let marick@gswd-vms.arpa know of any changes you
  273. ;;;  make. 
  274.  
  275.  
  276. (defun sshell-set-directory ()
  277.   (cond ((and (looking-at sshell-popd-regexp)
  278.           (memq (char-after (match-end 0)) '(?\; ?\n)))
  279.      (if sshell-directory-stack
  280.          (progn
  281.            (cd (car sshell-directory-stack))
  282.            (setq sshell-directory-stack (cdr sshell-directory-stack)))))
  283.     ((looking-at sshell-pushd-regexp)
  284.      (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  285.         (if sshell-directory-stack
  286.             (let ((old default-directory))
  287.               (cd (car sshell-directory-stack))
  288.               (setq sshell-directory-stack
  289.                 (cons old (cdr sshell-directory-stack))))))
  290.            ((memq (char-after (match-end 0)) '(?\  ?\t))
  291.         (let (dir)
  292.           (skip-chars-forward "^ ")
  293.           (skip-chars-forward " \t")
  294.           (if (file-directory-p
  295.             (setq dir
  296.                   (expand-file-name
  297.                 (substitute-in-file-name
  298.                   (buffer-substring
  299.                     (point)
  300.                     (progn
  301.                       (skip-chars-forward "^\n \t;")
  302.                       (point)))))))
  303.               (progn
  304.             (setq sshell-directory-stack
  305.                   (cons default-directory sshell-directory-stack))
  306.             (cd dir)))))))
  307.     ((looking-at sshell-cd-regexp)
  308.      (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  309.         (cd (getenv "HOME")))
  310.            ((memq (char-after (match-end 0)) '(?\  ?\t))
  311.         (let (dir)
  312.           (forward-char 3)
  313.           (skip-chars-forward " \t")
  314.           (if (file-directory-p
  315.             (setq dir
  316.                   (expand-file-name
  317.                 (substitute-in-file-name
  318.                   (buffer-substring
  319.                     (point)
  320.                     (progn
  321.                       (skip-chars-forward "^\n \t;")
  322.                       (point)))))))
  323.               (cd dir))))))))
  324.   
  325. (defun sshell-send-eof ()
  326.   "Send eof to subsshell (or to the program running under it)."
  327.   (interactive)
  328.   (process-send-eof))
  329.  
  330. (defun kill-output-from-sshell ()
  331.   "Kill all output from sshell since last input."
  332.   (interactive)
  333.   (goto-char (point-max))
  334.   (beginning-of-line)
  335.   (kill-region last-input-end (point))
  336.   (insert "*** output flushed ***\n")
  337.   (goto-char (point-max)))
  338.  
  339. (defun show-output-from-sshell ()
  340.   "Display start of this batch of sshell output at top of window.
  341. Also put cursor there."
  342.   (interactive)
  343.   (set-window-start (selected-window) last-input-end)
  344.   (goto-char last-input-end))
  345.  
  346. (defun copy-last-sshell-input ()
  347.   "Copy previous sshell input, sans newline, and insert before point."
  348.   (interactive)
  349.   (insert (buffer-substring last-input-end last-input-start))
  350.   (delete-char -1))
  351.  
  352. (defun interrupt-sshell-subjob ()
  353.   "Interrupt this sshell's current subjob."
  354.   (interactive)
  355.   (interrupt-process nil t))
  356.  
  357. (defun kill-sshell-subjob ()
  358.   "Send kill signal to this sshell's current subjob."
  359.   (interactive)
  360.   (kill-process nil t))
  361.  
  362. (defun quit-sshell-subjob ()
  363.   "Send quit signal to this sshell's current subjob."
  364.   (interactive)
  365.   (quit-process nil t))
  366.  
  367. (defun stop-sshell-subjob ()
  368.   "Stop this sshell's current subjob."
  369.   (interactive)
  370.   (stop-process nil t))
  371.  
  372. (defun kill-sshell-input ()
  373.   "Kill all text since last stuff output by the sshell or its subjobs."
  374.   (interactive)
  375.   (kill-region (process-mark (get-buffer-process (current-buffer)))
  376.            (point)))
  377. (require 'smart-complete)
  378.  
  379. (provide 'sshell)